/**
 * 2s 红灯，然后3s 绿灯， 然后 5s 黄灯
 */

class TrafficLight {
  constructor(name) {
    this.name = name;
    this.events = [];

    setTimeout(() => {
      this.next();
    }, 0);
  }

  lighting(color, timing) {
    this.events.push(() => {
      setTimeout(() => {
        console.log("当前color是", color);
        this.next();
      }, timing * 1000);
    });
    return this;
  }
  next() {
    const event = this.events.shift();
    event && event();
  }
}

const light = new TrafficLight("红绿灯");

light.lighting("red", 2).lighting("orange", 5).lighting("green", 3);
